package com.java110.mall.goods.cmd.productMenu;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.file.FileDto;
import com.java110.dto.housekeeping.HousekeepingTypeDto;
import com.java110.intf.goods.IHousekeepingTypeInnerServiceSMO;
import com.java110.intf.system.IFileInnerServiceSMO;
import com.java110.po.housekeeping.HousekeepingTypePo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 保存商品类型
 */
@Java110Cmd(serviceCode = "productMenu.saveProductMenu")
public class SaveProductMenuCmd extends Cmd {

    @Autowired
    private IHousekeepingTypeInnerServiceSMO housekeepingTypeInnerServiceSMOImpl;
    @Autowired
    private IFileInnerServiceSMO fileInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "hktName", "请求报文中未包含hktName");
        Assert.hasKeyAndValue(reqJson, "hktIcon", "请求报文中未包含hktIcon");
        Assert.hasKeyAndValue(reqJson, "seq", "请求报文中未包含seq");
        Assert.hasKeyAndValue(reqJson, "isShow", "请求报文中未包含isShow");
        Assert.hasKeyAndValue(reqJson, "url", "请求报文中未包含url");
        super.validateAdmin(context);
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        HousekeepingTypePo housekeepingTypePo = BeanConvertUtil.covertBean(reqJson, HousekeepingTypePo.class);
        housekeepingTypePo.setHktId(GenerateCodeFactory.getGeneratorId("11"));
        housekeepingTypePo.setTypeCd(HousekeepingTypeDto.TYPE_CD_MALL);
        housekeepingTypePo.setShopId("9999");
        if (housekeepingTypePo.getHktIcon().length() > 512) {
            FileDto fileDto = new FileDto();
            fileDto.setContext(housekeepingTypePo.getHktIcon());
            fileDto.setFileName(housekeepingTypePo.getHktId());
            String fileName = fileInnerServiceSMOImpl.saveFile(fileDto);
            housekeepingTypePo.setHktIcon(fileName);
        }
        int flag = housekeepingTypeInnerServiceSMOImpl.saveHousekeepingType(housekeepingTypePo);
        if (flag < 1) {
            throw new CmdException("保存失败");
        }

    }
}
